package cn.czg.cms.controller;

import cn.czg.cms.common.ResultT;
import cn.czg.cms.entity.User;
import cn.czg.cms.entity.req.LoginRequest;
import cn.czg.cms.entity.req.RegisterRequest;
import cn.czg.cms.service.UserService;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ResultT<String> register(@RequestBody RegisterRequest req) {
        boolean ok = userService.register(req.getUsername(), req.getPassword(), req.getNickname());
        if (ok) {
            return ResultT.success("注册成功");
        } else {
            return ResultT.error("用户名已存在");
        }
    }

    @PostMapping("/login")
    public ResultT<Map<String, Object>> login(@RequestBody LoginRequest req) {
        User user = userService.login(req.getUsername(), req.getPassword());
        if (user != null) {
            // 登录成功，使用Sa-Token登录，参数通常用用户ID
            StpUtil.login(user.getId());

            // 获取token
            String token = StpUtil.getTokenValue();

            // 不返回密码字段
            user.setPasswordHash(null);

            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("user", user);

            return ResultT.success(data);
        } else {
            return ResultT.error("用户名或密码错误");
        }
    }

    @GetMapping("/{id}")
    @SaCheckLogin // 需要登录才能访问
    public ResultT<User> getById(@PathVariable Long id) {
        User user = userService.getById(id);
        if (user != null) {
            user.setPasswordHash(null);
            return ResultT.success(user);
        } else {
            return ResultT.error("用户不存在");
        }
    }

    @GetMapping("/exists")
    public ResultT<User> exists(String phone) {
        User user = userService.findByPhone(phone);
        if (user != null) {
            user.setPasswordHash(null);
            return ResultT.success(user);
        } else {
            return ResultT.error("用户不存在");
        }
    }

    @PostMapping("/updateNickname")
    public ResultT<User> updateNickname(String id, String nickName) {
        userService.updateNickNameById(id, nickName);
        return ResultT.success();
    }

    @PostMapping("/updateGender")
    public ResultT<User> updateGender(String id, int gender) {
        userService.updateGender(id, gender);
        return ResultT.success();
    }


    @PostMapping("/updateAvatar")
    public ResultT<User> updateAvatar(String id, String avatar) {
        userService.updateAvatar(id, avatar);
        return ResultT.success();
    }


    @PostMapping("/logout")
    @SaCheckLogin
    public ResultT<String> logout() {
        StpUtil.logout();
        return ResultT.success("退出登录成功");
    }
}
